#include<bits/stdc++.h>
#include<bits/extc++.h>
#define int long long
using namespace std;
using namespace __gnu_pbds;
const int N=5e5+10;
int n,x[N],y[N],ans,dis[4010],sum[N],siz[N];
gp_hash_table<int,int> mp;
vector<int> edge[N];
queue<int> q;
inline int get(int x,int y){
	return x*1000000ll+y;
}
inline void work(int x){
	while(!q.empty())
		q.pop();
	q.push(x);
	memset(dis,127,sizeof(dis));
	dis[x]=0;
	while(!q.empty()){
		int now=q.front();
		q.pop();
		for(auto &i:edge[now])
			if(dis[i]>1e18) dis[i]=dis[now]+1,ans+=dis[i],q.push(i);
	}
}
inline void dfs1(int u,int f){
	siz[u]=1;
	for(auto &i:edge[u])
		if(i!=f){
			dfs1(i,u);
			siz[u]+=siz[i],sum[u]+=sum[i]+siz[i];
		}
}
inline void dfs2(int u,int f,int now){
	ans+=sum[u]+now;
	for(auto &i:edge[u])
		if(i!=f) dfs2(i,u,now+sum[u]-(sum[i]+siz[i])+(n-siz[i]));
}
signed main(){
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x[i]>>y[i];
		mp[get(x[i],y[i])]=i;
	}
	int flg=1;
	for(int i=1;i<=n;i++)
		if(mp[get(x[i]+1,y[i])]&&mp[get(x[i],y[i]+1)]&&mp[get(x[i]+1,y[i]+1)]) flg=0;
	if(n<=4000){
		for(int i=1;i<=n;i++){
			if(mp[get(x[i]+1,y[i])]) edge[i].push_back(mp[(get(x[i]+1,y[i]))]);
			if(mp[get(x[i],y[i]+1)]) edge[i].push_back(mp[(get(x[i],y[i]+1))]);
			if(mp[get(x[i]-1,y[i])]) edge[i].push_back(mp[(get(x[i]-1,y[i]))]);
			if(mp[get(x[i],y[i]-1)]) edge[i].push_back(mp[(get(x[i],y[i]-1))]);
		}
		for(int i=1;i<=n;i++)
			work(i);
		ans/=2;
	}else if(flg){
		for(int i=1;i<=n;i++){
			if(mp[get(x[i]+1,y[i])]) edge[i].push_back(mp[(get(x[i]+1,y[i]))]);
			if(mp[get(x[i],y[i]+1)]) edge[i].push_back(mp[(get(x[i],y[i]+1))]);
			if(mp[get(x[i]-1,y[i])]) edge[i].push_back(mp[(get(x[i]-1,y[i]))]);
			if(mp[get(x[i],y[i]-1)]) edge[i].push_back(mp[(get(x[i],y[i]-1))]);
		}
		dfs1(1,0),dfs2(1,0,0);
		ans/=2;
	}else{
		int sum1=0,sum2=0;
		sort(x+1,x+n+1),sort(y+1,y+n+1);
		for(int i=1;i<=n;i++)
			sum1+=x[i],sum2+=y[i],ans+=(x[i]+y[i])*i-sum1-sum2;
	}
	cout<<ans;
	return 0;
}